package com.oasys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.oasys.dao.DeptMapper;
import com.oasys.entity.po.DeptPo;
import com.oasys.entity.vo.DeptVo;
import com.oasys.enums.OaResponseCode;
import com.oasys.service.DeptService;
import com.oasys.utils.AutoGenerateIds;
import com.oasys.utils.ResponseResult;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: TODO
 * @Author: mis_wu
 * @Date: 2022/5/15
 **/
@Service
@Transactional(rollbackFor = Exception.class)
public class DeptServiceImpl implements DeptService {

    @Resource
    private DeptMapper deptMapper;

    /**
     * dept
     * @param deptVo
     * @return
     */
    @Override
    public ResponseResult<Map<String,Object>> getDeptInfo(DeptVo deptVo) {
        try {
            Map<String,Object> map = new HashMap<>(8);
            PageHelper.startPage(deptVo.getPage(),deptVo.getSize());
            List<DeptPo> deptInfo = deptMapper.getDeptInfo(deptVo.getDeptName());
            PageInfo<DeptPo> pageInfo = new PageInfo<>(deptInfo);
            map.put("deptList",pageInfo.getList());
            map.put("totals",pageInfo.getTotal());
            return new ResponseResult<>(OaResponseCode.SUCCESS.getCode(),OaResponseCode.SUCCESS.getMsg(),map);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * dept
     * @param deptName
     * @return
     */
    @Override
    public ResponseResult<Boolean> addDept(String deptName) {
        try {
            DeptPo deptPo = new DeptPo();
            deptPo.setDeptId(AutoGenerateIds.getGenerateId());
            deptPo.setDeptName(deptName);
            Boolean result = deptMapper.addDept(deptPo);
            return new ResponseResult<>(OaResponseCode.SUCCESS.getCode(),OaResponseCode.SUCCESS.getMsg(),result);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * up
     * @param deptPo
     * @return
     */
    @Override
    public ResponseResult<Boolean> updateDept(DeptPo deptPo) {
        try {
            Boolean result;
            if (ObjectUtils.isEmpty(deptPo)){
                result = false;
            }
            result = deptMapper.updateDept(deptPo);
            return new ResponseResult<>(OaResponseCode.SUCCESS.getCode(),OaResponseCode.SUCCESS.getMsg(),result);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * del
     * @param deptId
     * @return
     */
    @Override
    public ResponseResult<Boolean> delDept(Long deptId) {
        try {
            Boolean result;
            if (ObjectUtils.isEmpty(deptId)){
                result = false;
            }
            result = deptMapper.delDept(deptId);
            return new ResponseResult<>(OaResponseCode.SUCCESS.getCode(),OaResponseCode.SUCCESS.getMsg(),result);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
